package com.onlinevideo.service.impl;

import com.onlinevideo.dao.VideoItemDao;
import com.onlinevideo.dao.impl.VideoItemDaoImpl;
import com.onlinevideo.entity.VideoItem;
import com.onlinevideo.service.VideoItemService;
import com.onlinevideo.util.FileUpLoadUtils;
import org.apache.commons.fileupload.FileItem;

import java.io.File;
import java.util.List;
import java.util.Random;

public class VideoItemServiceImpl implements VideoItemService {

    private final VideoItemDao videoItemDao = new VideoItemDaoImpl();

    @Override
    public VideoItem insert(List<FileItem> fileItems) {
        VideoItem videoItem = null;
        try {
            FileItem videoItem1 = FileUpLoadUtils.find(fileItems, "videoItem");
            FileItem videoId = FileUpLoadUtils.find(fileItems, "videoId");
            String name = videoItem1.getName();
            String[] split = name.split("_");
            if (split.length >= 2 || split[0].matches("[0-9]+")) {
                videoItem = new VideoItem();
                long abs = System.currentTimeMillis()+ new Random().nextInt(2000000000);
                videoItem.setId(abs);
                videoItem.setSortCount(Integer.valueOf(split[0]));
                videoItem.setName(split[1]);
                videoItem.setVideoId(Long.valueOf(videoId.getString()));
                String videoUrl = FileUpLoadUtils.getVideoName(name);
                videoItem.setUrl(videoUrl);
                videoItem1.write(new File(videoUrl));
                videoItemDao.insert(videoItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return videoItem;
    }

    @Override
    public int insert(VideoItem videoItem) {
        return videoItemDao.insert(videoItem);
    }

    @Override
    public int delete(Long id) {
        VideoItem videoItem = queryById(id);
        if (videoItem == null) {
            return 0;
        }
        boolean delete = new File(videoItem.getUrl()).delete();
        return videoItemDao.delete(id);
    }

    @Override
    public int deleteByVideoId(Long id) {
        List<VideoItem> videoItems = queryByVideoId(id);
        for (VideoItem videoItem : videoItems) {
            new File(videoItem.getUrl()).delete();
            videoItemDao.delete(videoItem.getId());
        }
        return 1;
    }

    @Override
    public int update(List<FileItem> fileItems) {
        try {
            FileItem videoItemId = FileUpLoadUtils.find(fileItems, "videoItemId");
            delete(Long.valueOf(videoItemId.getString()));
            insert(fileItems);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }
    @Override
    public int update(String name,Integer sortCount,Long id) {
        return videoItemDao.update(name,sortCount,id);
    }

/*    @Override
    public String updateFile(List<FileItem> fileItems) {
        try {
            if (fileItems == null) {
                return null;
            }
            FileItem fileItem = FileUpLoadUtils.find(fileItems, "videoItem");
            FileItem id = FileUpLoadUtils.find(fileItems, "videoItemId");
            if (fileItem != null && id != null) {
                VideoItem videoItem = queryById(Long.valueOf(id.getString()));
                //todo:可能会出现空指针
                String url = videoItem.getUrl();
                File file = new File(url);
                boolean delete = file.delete();
                if (delete) {
                    fileItem.write(file);
                    return url;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }*/

    @Override
    public VideoItem queryById(Long id) {
        return videoItemDao.queryById(id);
    }

    @Override
    public List<VideoItem> queryAll() {
        return videoItemDao.queryAll();
    }

    @Override
    public List<VideoItem> queryByVideoId(Long id) {
        return videoItemDao.queryByVideo(id);
    }
}
